layui.use(['form','layer','table','util'],function (){
    let form = layui.form,layer = layui.layer,table = layui.table,util = layui.util;

    //监听table 工具栏，修改受理状态：档案数字化、数字化完成
    table.on('toolbar(fistDataTable)',function (obj) {

        //获取监听事件
        let layEvent = obj.event;
        //获取所有的选中的数据
        let borrowData = layui.table.checkStatus("dataTableId").data;
        if (borrowData.length <= 0){
            layer.msg("请选择一条数据",{icon:3,time:1500});
            return;
        }
        //获取归还信息主键集合
        let bwIdList = getBwId(borrowData);
        let jsonData = {
            "bwIdList" : bwIdList
        }
        sendAjax("/archivesReadIn/readIn",jsonData,function (res) {
            if (valueIsEqual(res.code,"00")){
                top.layer.msg("档案借入成功",{icon:6,time:1500});
            }else {
                top.layer.msg("档案借入失败！",{icon:6,time:1500});
            }
            queryBorrowList();
        })
    });

})

/**
 * @author: ChengJW
 * @params:
 * @return:
 * @date: 2021/9/22
 * @description: 初始化生成table数据
 */
$(function () {
    queryBorrowList();
})

/**
 * @author: ChengJW
 * @params:
 * @return:
 * @date: 2021/9/22
 * @description: 生成table数据
 */
function queryBorrowList() {

    //查询条件类型
    let srchReqFir = $("select[name = 'srchReqFir']").val();
    //查询条件
    let qryReqFir = $("input[name = 'qryReqFir']").val().trim();
    let jsonData = {
        "srchReqFir" : srchReqFir,
        "qryReqFir" : qryReqFir
    }
    //时间戳
    let tableTitle = '借入档案信息表'+layui.util.toDateString(new Date(),'yyyyMMddHHmmss');
    layui.table.render({
        elem:'#dataTable',
        id:'dataTableId',
        url:'/archivesReadIn/queryBorrowInfoList',
        parseData:function (result) {
            return{
                "code":result.code,
                "msg":result.msg,
                "count":result.data.pageCount,
                "data":result.data.list
            }
        },
        size:'sm',
        //分页的起止页号，每页条数
        page:{
            curr:1,
            limit:15,
            limits:[15,30,50,100]
        },
        //自定义的页码参数名称、每页条数参数名称
        request:{
            pageName:'pageNum',
            limitName:'pageSize'
        },
        //table的高度
        height:500,
        //查询条件，json格式
        where:jsonData,
        cols:[[
            {type:'checkbox'},
            {type:'numbers',title:'序号'},
            {field:'bwCmRegistId',title:'注册号'},
            {field:'bwCmCreditCode',title:'统一社会信用代码',width: 180},
            {field:'bwCmNm',title:'企业名称',width:300},
            {field:'bwCmArchType',title:'档案类型',width: 100},
            {field:'bwCmTotalNum',title:'页数',width: 70,templet:function (d) {
                    return d.bwCmTotalNum + '页';
                }},
            {field:'bwCmCheckDt',title:'核准日期',width:100,templet:function (d) {
                    return layui.util.toDateString(d.bwCmCheckDt,"yyyy-MM-dd");
                }},
            {field:'bwRealNm',title:'出借人',width: 70},
            {field:'bwDt',title:'出借日期',width: 100},
            {field:'bwReadNm',title:'借阅人',width: 70},
            {field:'bwReadDt',title:'归还日期',width: 100,templet:function (d){
                 let bwReadDt01 = layui.util.toDateString(d.bwReadDt,"yyyyMMdd");
                 let nowDate = layui.util.toDateString(new Date(),"yyyyMMdd");
                 if (bwReadDt01 < nowDate){
                     return '<span style="color: red">'+d.bwReadDt+'</span>';
                 }else {
                     return d.bwReadDt;
                 }
                }},
            {field: 'bwDesc',title: '出借说明'},
            {field:'bwId',title:'主键',hide:true},
        ]],
        //头部工具条
        toolbar: '#totalToolBar',
        //导出时候excel文件标题
        title:tableTitle
    });
}

/**
 * @author: ChengJW
 * @params:
 * @return:
 * @date: 2021/12/2
 * @description: 获取选中行的主键号集合
 */
function getBwId(borrowData) {
    let bwIdList = "";
    for (let i=0;i < borrowData.length; i++){
        if (valueIsEqual(bwIdList,"")){
            bwIdList = borrowData[i].bwId ;
        }else {
            bwIdList = bwIdList + "," + borrowData[i].bwId;
        }
    }
    return bwIdList;
}